﻿using System;
using Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling;
using Microsoft.ServiceBus.Messaging;
using Nardax.Azure.ServiceBus;
using Nardax.Samples.ServiceBus;

namespace Nardax.Samples.ServiceBusSubscriptionAdapter
{
    class Program
    {
        static void Main(string[] args)
        {
            // could be just one or more than two
            var connectionStrings = new[] { "Endpoint One", "Endpoint Two" };
            var retryPolicy = new RetryPolicy(new ServiceBusTransientErrorDetectionStrategy(), RetryStrategy.DefaultExponential);
            var topicDescription = new TopicDescription("RP-TEST");
            var subscriptionDescription = new SubscriptionDescription("RP-TEST", "Sample");
            var nardaxBrokeredMessageFactory = new NardaxBrokeredMessageFactory();
            var prefetchCount = 20;
            var serverWaitTime = new TimeSpan(0, 0, 10);

            var subscriptionAdapterDescription = new SubscriptionAdapterDescription<INardaxBrokeredMessage>
                {
                    ConnectionStrings = connectionStrings,
                    TopicDescription = topicDescription,
                    SubscriptionDescription = subscriptionDescription,
                    RetryPolicy = retryPolicy,
                    BrokeredMessageContainerFactory = nardaxBrokeredMessageFactory,
                    PrefetchCount = prefetchCount,
                    ServerWaitTime = serverWaitTime
                };

            var subscriptionAdapter = new SubscriptionAdapter<INardaxBrokeredMessage>(subscriptionAdapterDescription);

            var messageProcessor = new MessageProcessor(subscriptionAdapter);
            messageProcessor.ProcessMessages();

            Console.ReadKey();
        }
    }
}
